package edu.hubu.book.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.hubu.book.domain.Page;
import edu.hubu.book.service.SearchService;
import edu.hubu.book.utils.JsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author waxijiang
 */
@Controller
@RequestMapping("/search")
public class SearchController {

    final
    SearchService searchService;

    public SearchController(SearchService searchService) {
        this.searchService = searchService;
    }

    /**
     * 根据书名, 一级类名, 二级类名分页查找数据
     *
     * @param bookName        书名
     * @param firstClassName  一级类名
     * @param secondClassName 二级类名
     * @param pageSize        单页书的数量
     * @param session         session对象
     * @return 封装好的page
     */
    @RequestMapping("")
    public @ResponseBody
    Page search(@RequestParam(value = "bookName", required = false) String bookName,
                @RequestParam(value = "firstClassName", required = false) String firstClassName,
                @RequestParam(value = "secondClassName", required = false) String secondClassName,
                Integer pageSize,
                HttpSession session) {
        // 当没有传入pageSize或者传入出错时, 使用默认值
        if (pageSize == null || pageSize < 0) {
            pageSize = 12;
        }
        session.setAttribute("pageSize", pageSize);
        // 一级分类id
        Integer sid = null;
        // 二级分类id
        Integer ssid = null;

        // 将bookName存入session
        if (bookName != null) {
            session.setAttribute("bookName", bookName.trim());
        }
        // 将二级分类id存入session
        if (secondClassName != null) {
            // 当传进来的不是空串
            if (!"".equals(secondClassName)) {
                ssid = searchService.findIdBySecondClassName(secondClassName.trim());
                // 当传入的类名无法匹配任何分类
                if (ssid == null) {
                    ssid = -1;
                }
            }
            session.setAttribute("ssid", ssid);
        } else {
            // 将一级分类id存入session
            if (firstClassName != null) {
                // 当传进来的不是空串
                if (!"".equals(firstClassName)) {
                    sid = searchService.findIdByFirstClassName(firstClassName.trim());
                    // 当传入的类名无法匹配任何分类
                    if (sid == null) {
                        sid = -1;
                    }
                }
                session.setAttribute("sid", sid);
            }
        }
        // 查找书籍
        return searchService.findByPage(1, pageSize, bookName, sid, ssid);
    }

    /**
     * 根据页数来查找书籍信息
     *
     * @param pageId  页码
     * @param session session
     * @return 封装好的page对象
     */
    @RequestMapping(value = "/{pageId}", method = RequestMethod.GET)
    public @ResponseBody
    Page getPage(@PathVariable("pageId") Integer pageId, HttpSession session) {
        if (pageId <= 0) {
            return null;
        }
        String bookName = (String) session.getAttribute("bookName");
        Integer sid = (Integer) session.getAttribute("sid");
        Integer ssid = (Integer) session.getAttribute("ssid");
        Integer pageSize = (Integer) session.getAttribute("pageSize");
        // 防止出错
        if (pageSize == null) {
            pageSize = 12;
        }
        return searchService.findByPage(pageId, pageSize, bookName, sid, ssid);
    }

    /**
     * 查找所有一级分类
     *
     * @return 一级分类列表
     */
    @RequestMapping(value = "/firstClass", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody
    String getAllFirstClass() {
        // 获取所有的分类
        List<String> firstClassList = searchService.getAllFirstClass();
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        map.put("Class", firstClassList);
        return JsonUtils.mapToJson(map);
    }


    /**
     * 根据一级分类类名查找二级分类
     *
     * @param firstClassName 一级分类类名
     * @return 二级分类列表
     */
    @RequestMapping(value = "/secondClass", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
    public @ResponseBody
    String getSecondClass(@RequestParam(value = "firstClassName", required = false) String firstClassName) {
        // 根据第一类类名查找sid
        Integer sid = searchService.findIdByFirstClassName(firstClassName);
        List<String> secondClassList = searchService.getSecondClass(sid);
        Map<String, List<String>> map = new HashMap<String, List<String>>();
        map.put("Class", secondClassList);
        return JsonUtils.mapToJson(map);
    }
}
